<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class BasicTitleInfo extends Model
{
    protected $table = 'title_info'; 
    protected $guarded = [];
    protected $hidden = [
        'created_at',
        'updated_at',
        'creator_id',
        'updater_id',
    ];
    protected $appends = [
        'title_set',
        'rank_set',
        'manage_title_set',
        'manage_rank_set',
        'employ_year',
    ];

    public function getTitleSetAttribute(){
        return $this->getFieldValue('tea_title');
    }
    public function getRankSetAttribute(){
        return $this->getFieldValue('tea_title_rank');
    }
    public function getManageTitleSetAttribute(){
        return $this->getFieldValue('manage_title');
    }
    public function getManageRankSetAttribute(){
        return $this->getFieldValue('manage_title_rank');
    }

    public function getFieldValue($field)
    {
        $f = !empty($this->attributes[$field]) ? $this->attributes[$field] : '';

        if(!$f){
            return [];
        }

        return json_decode($f, true);  
    }

       //聘任年限
       public function getEmployYearAttribute(){
        $result = 0;

        $workDate = !empty($this->attributes['tea_employ_time']) ? $this->attributes['tea_employ_time'] : '';
        if(!$workDate){
            return $result;
        }

        $result = Carbon::parse($workDate)->age;
        
        return $result;
    }
}
